package com.su.test.testdemo;


import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.MultiAutoCompleteTextView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.su.test.testdemo.base.BaseActivity;

public class MainActivity extends BaseActivity {

    private Button insert;

    private Button get;

    private String TAG = "MainActivity";

    private int id = 0;

    private IPCPersonProvider personProvider;

    private MultiAutoCompleteTextView host;
    private MultiAutoCompleteTextView port;
    private MultiAutoCompleteTextView channel;
    private MultiAutoCompleteTextView debug;
    private MultiAutoCompleteTextView push;
    private MultiAutoCompleteTextView encrypt;

    private Button start;

    Uri uri = IPCPersonProvider.PERSON_CONTENT_URI;    //ContentProvider 中注册的 URI

    private String hosts[] = {"appapi.v2.babystory365.com", "appapi.newtest.babystory365.com", "appapi.pre.babystory365.com"};
    private String ports[] = {"80","50051","50052"};
    private String channels[] = {"official","baidu","xiaomi","test","huawei","oppo","vivo","qh360","yingyongbao","lenovo","wandoujia","oppo"
            ,"meizu","taobao","mm","wo","anzhishichang","nduoa"
            ,"yyhui","samsung"};
    private String pushs[] = {"true","false"};
    private String debugs[] = {"true","false"};
    private String encrypts[] = {"true","false"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        personProvider = new IPCPersonProvider();
        initView();

        setmultitext();

    }

    private void initView() {
        insert = (Button) findViewById(R.id.button_insert);
        get = (Button) findViewById(R.id.button_get);
        host = findViewById(R.id.host_tv);
        port = findViewById(R.id.port_tv);
        channel = findViewById(R.id.channel_tv);
        debug = findViewById(R.id.debug_tv);
        start = findViewById(R.id.go_start);
    }

    private void setmultitext() {
        host.setAdapter(new ArrayAdapter(this,R.layout.item_list,hosts));
        // 设置触发提示阈值
        host.setThreshold(1);
        host.setTokenizer(new ShellTokenizer());
        host.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                host.showDropDown();
            }
        });
        host.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!host.getText().equals("")){
                    host.setText(hosts[position]);
                }
            }
        });

        port.setAdapter(new ArrayAdapter(this,R.layout.item_list,ports));
        // 设置触发提示阈值
        port.setThreshold(1);
        port.setTokenizer(new ShellTokenizer());
        port.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                port.showDropDown();
            }
        });
        port.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!port.getText().equals("")){
                    port.setText(ports[position]);
                }
            }
        });

        channel.setAdapter(new ArrayAdapter(this,R.layout.item_list,channels));
        // 设置触发提示阈值
        channel.setThreshold(1);
        channel.setTokenizer(new ShellTokenizer());
        channel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                channel.showDropDown();
            }
        });
        channel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!channel.getText().equals("")){
                    channel.setText(channels[position]);
                }
            }
        });

        debug.setAdapter(new ArrayAdapter(this,R.layout.item_list,debugs));
        // 设置触发提示阈值
        debug.setThreshold(1);
        debug.setTokenizer(new ShellTokenizer());
        debug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                debug.showDropDown();
            }
        });
        debug.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!debug.getText().equals("")){
                    debug.setText(debugs[position]);
                }
            }
        });

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insertData();
                startHandler.sendEmptyMessageDelayed(1,1000);
            }
        });
    }
    private Handler startHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            PackageManager packageManager = getPackageManager();
            Intent intent=new Intent();
            intent =packageManager.getLaunchIntentForPackage("com.talkweb.babystorys");
            if(intent==null){
                Toast.makeText(MainActivity.this, "未安装", Toast.LENGTH_LONG).show();
            }else{
                startActivity(intent);
            }
        }
    };
    private void insertData() {
        /**
         *  test.push TEXT,
         *  test.host TEXT,
         *  test.port TEXT,
         *  test.debug TEXT,
         *  test.encrypt TEXT,
         *  test.channel TEXT
         */
        ContentValues contentValues = new ContentValues();
        contentValues.put("_id", id++);
        contentValues.put("push", "false");
        contentValues.put("host", host.getText().toString().trim());
        contentValues.put("port", port.getText().toString().trim());
        contentValues.put("debug", debug.getText().toString().trim());
        contentValues.put("encrypt", "true");
        contentValues.put("channel", channel.getText().toString().trim());
        ContentResolver contentResolver = getContentResolver();    //获取内容处理器
        contentResolver.delete(uri,null,null);
        contentResolver.insert(uri, contentValues);    //插入一条数据
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void getContentFromContentProvider() {
        ContentResolver contentResolver = getContentResolver();    //获取内容处理器
        /**
         *  test.push TEXT,
         *  test.host TEXT,
         *  test.port TEXT,
         *  test.debug TEXT,
         *  test.encrypt TEXT,
         *  test.channel TEXT
         */
        //再查询一次
        Cursor cursor = contentResolver.query(uri, new String[]{"push", "host", "port", "debug", "encrypt", "channel"}, null, null, null, null);
        if (cursor == null) {
            return;
        }
        StringBuilder cursorResult = new StringBuilder("DB 查询结果：");
        while (cursor.moveToNext()) {
            String result = cursor.getString(0) + ", " + cursor.getString(1)+ ", " +
                    cursor.getString(2)+ ", " + cursor.getString(3)+ ", " + cursor.getString(4)+ ", " + cursor.getString(5);
            Log.d(TAG, "DB 查询结果：" + result);
            cursorResult.append("\n").append(result);
        }
        get.setText(cursorResult.toString());
        cursor.close();
    }
}
